<?php

/**
 * @Author: DEEP-2018BRTYLP\Administrator
 * @Date:   2019-03-19 15:17:42
 * @Last Modified by:   DEEP-2018BRTYLP\Administrator
 * @Last Modified time: 2019-04-01 11:43:55
 */
namespace app\api\controller;
use app\common\model\Version;
use fast\Random;
use think\Config;
use app\api\controller\Base;
/**
 * 
 */
class Member extends Base
{
	/*
		列表
	 */ 
	public function list()
	{
		$page = input('param.page',1);
		$num = 6;
		$map['status'] = 1;
		$data = db('member')->where($map)->order('id desc')->cache(true,60)->page($page,$num)->field('id,avatar,live_city,user_name')->select();
 		foreach ($data as $key => &$value) {
			$value['avatar'] = "http://".$_SERVER['SERVER_NAME'].$value['avatar'];
			if($value['live_city']){
				$value['live_city'] = explode('/',$value['live_city'])[1];
			}else{
				$value['live_city'] = "未知城市";
			}
		}
		//dump($data);exit;
		if($data){
			return $this->return_success('获取数据',$data,1);
		}else{
			return $this->return_error('暂无数据',[],0);
		}
	}

	/*
		详情
	 */
	
	public function detail(){
		$id = input('param.id',1);
		$data = db('member')->where(['id'=>$id])->cache(true,60)->find();
		$data["birthday"] = get_age($data["birthday"]);
		$data["live_city"] = city($data["live_city"]);
		$data["height"] = height($data["height"]);
		$data["weight"] = weight($data["weight"]);
		$data["education_id"] = education($data["education_id"]);
		$data["income_id"] = income($data["income_id"]);
		$data["zodiac_id"] = zodiac($data["zodiac_id"]);
		$data["house_id"] = house($data["house_id"]);
		$data["marriage_id"] = marriage($data["marriage_id"]);
		$data["car_id"] = car($data["car_id"]);
		$data["tags_ids"] = get_tags($data["tags_ids"]);
		$data["gender"] = gender($data["gender"]);
		$data['images'] = explode(',',$data['images']);
		$data['person_data'][] = $data["live_city"];
		$data['person_data'][] = $data["weight"];
		$data['person_data'][] = $data["education_id"];
		$data['person_data'][] = $data["income_id"];
		$data['person_data'][] = $data["house_id"];
		$data['person_data'][] = $data["car_id"];
		$data['person_data'][] = $data["marriage_id"];
		$mate = db('member_mate')->where(['uid'=>$id])->find();
		if($mate){
			$data['mate_data'][] = get_age1($mate["age_id"]);
			$data['mate_data'][] = get_height1($mate["height_id"]);
			$data['mate_data'][] = car($mate["car_id"]);
			$data['mate_data'][] = house($mate["house_id"]);
			$data['mate_data'][] = income($mate["income_id"]);
			$data['mate_data'][] = education($mate["education_id"]);
			$data['mate_data'][] = marriage($mate["marriage_id"]);
			if($mate['question']){
				$data['question'] = json_decode($mate["question"]);
			}else{
				$data['question']= [];
			}
		}else{
			$data['mate_data'] = [];
			$data['question']= [];
		}
		
		if($data){
			return $this->return_success('获取数据',$data,1);
		}else{
			return $this->return_error('暂无数据',[],0);
		}
	}


	/**
	 * 提交个人信息
	 */
	public function sub_info(){
		$uid = input('param.id',1);
		$json_str = input('param.json_str');
		$m_json_str = input('param.m_json_str');
		$images = input('param.images');
		$question = input('param.question');
		$images_str = explode(',',$images);
		$member_data = json_decode($json_str,true);
		$m_member_data = json_decode($m_json_str,true);
		$member_data['birthday'] = substr($member_data['birthday'],0,10);
		$member_data['birth_city'] = implode('/',explode('-',$member_data['birth_city']));
		$member_data['live_city'] = implode('/',explode('-',$member_data['live_city']));
		$member_data['last_login_time'] = time();
		$m_member_data['create_time'] = time();
		$m_member_data['question'] = $question;
		$re = db('member')->where(['id'=>$uid])->update($member_data);
		$m_re = db('member_mate')->where(['uid'=>$uid])->find();
		if($m_re){
			$m_re = db('member_mate')->where(['uid'=>$uid])->update($m_member_data);
		}else{
			$m_member_data['uid'] = $uid;
			$m_re = db('member_mate')->insert($m_member_data);

		}
		if($re){
			return $this->return_success('保存成功',[],1);
		}else{
			return $this->return_error('保存失败',[],0);
		}
	}

	/**
	 * 获取用户信息
	 */
	public function user_info(){
		$uid = input('param.uid',1);
		$member_data = db('member')->where(['id'=>$uid])->field('birthday,images,avatar,user_name,gender,wechat,birth_city,live_city,height,weight,education_id,income_id,zodiac_id,marriage_id,car_id,house_id,worker,real_name,id_images,id_images1,id_number,authen_status,flower_num,mobile,hide_status')->find();
		$member_data['images'] = explode(',',$member_data['images']);
		$member_data['birth_city'] = implode('-',explode('/',$member_data['birth_city']));
		$member_data['live_city'] = implode('-',explode('/',$member_data['live_city']));
		if($member_data['gender'] == 0){
			$member_data['gender'] = '男';
		}else{
			$member_data['gender'] = '女';
		}
		$member_data['is_pick'] = $this->is_pick($uid);
		$m_member_data = db('member_mate')->where(['uid'=>$uid])->find();
		$m_member_data['question'] = json_decode($m_member_data['question'],true);
		return $this->return_success('获取数据成功',['m_member_data'=>$m_member_data,'member_data'=>$member_data],1);
	}

	public function get_user($uid){
		$member_data = db('member')->where(['id'=>$uid])->field('birthday,images,birth_city,live_city,height,weight,education_id,income_id,zodiac_id,marriage_id,car_id,house_id,worker,real_name,id_images,id_images1,id_number,authen_status,flower_num')->find();
		$member_data['images'] = explode(',',$member_data['images']);
		$member_data['birth_city'] = implode('-',explode('/',$member_data['birth_city']));
		$member_data['live_city'] = implode('-',explode('/',$member_data['live_city']));
		return $member_data;
	}

	/**
	 * 提交认证信息
	 */
	public function authen(){
		$uid = input('param.uid',1);
		$real_name = input('param.real_name');
		$id_number = input('param.id_number');
		$id_images = input('param.id_images');
		$id_images1 = input('param.id_images1');
		$data = [
			'real_name' => $real_name,
			'id_number' => $id_number,
			'id_images' => $id_images,
			'authen_status' => 1,//待认证
			'id_images1' => $id_images1,
			'last_login_time' => time(),
		];
		//dump($data);exit;
		$re = db('member')->where(['id'=>$uid])->update($data);
		if($re){
			return $this->return_success('提交成功！',[],1);
		}else{
			return $this->return_error('提交失败！',[],0);
		}
	}

	public function weixin(){
		$id = input('param.id');
		$str = "<div class='mask'><div class='mask_content'><div class='mask_close'>X</div><div class='mask_title'>TA的微信号</div><div class='mask_num'>num123nk</div><div class='mask_copy' data-clipboard-action='copy' data-clipboard-target='.mask_num'>复制微信号</div></div></div>";
		return $this->return_success('提交成功！',$str,1);
	}

	/**
	 * 摘取
	 * @return [type] [description]
	 */
	public function pick(){
		$uid = input('param.uid',1);
		$week = input('param.day');
		$date = date('Y-m-d',time());
		$date = explode('-',$date);
		$data = [
			'uid' => $uid,
			'week'=> $week,
			'year' => $date[0],
			'month' => $date[1],
			'day' => $date[2],
			'createtime' => time(),
		];
		$map['uid'] = $uid;
		$map['year'] = $date[0];
		$map['month'] = $date[1];
		$map['day'] = $date[2];
		$map['week'] = $week;
		$is_pick = db('flowers')->where($map)->find();
		if($is_pick){
			return $this->return_success('今天已摘取',['is_pick'=>1],1);
		}else{
			$re = db('flowers')->insert($data);
		}
		if($re){
			db('member')->where(['id'=>$uid])->setInc('flower_num');
			$user_info = $this->get_user($uid);
			return $this->return_success('摘取成功！',['is_pick'=>1,'user_info'=>$user_info],1);
		}else{
			return $this->return_error('摘取失败',[],0);
		}
	}
	
	/**
	 * 判断是否摘取
	 */
	public function is_pick($uid){
		$date = date('Y-m-d',time());
		$date = explode('-',$date);
		$map['uid'] = $uid;
		$map['year'] = $date[0];
		$map['month'] = $date[1];
		$map['day'] = $date[2];
		$is_pick = db('flowers')->where($map)->find();
		if($is_pick){
			return 1;
		}else{
			return 0;
		}

	}

	/**
	 * 设置显示隐藏
	 */
	public function hide(){
		$uid = input('param.uid',1);
		$hide_status = input('param.hide_status');
		$re = db('member')->where(['id'=>$uid])->update(['last_login_time'=>time(),'hide_status'=>$hide_status]);
		if($re){
			return $this->return_success('设置成功!',[],1);
		}else{
			return $this->return_error('设置失败!',[],0);
		}

	}

	/**
	 * 上传信息
	 */
	public function up(){
		$uid = input('param.uid',1);
		$user_name = input('param.user_name');
		$wechat = input('param.wechat');
		$avatar = input('param.avatar');
		$data = [
			'user_name' => $user_name,
			'wechat' => $wechat,
			'avatar' => $avatar,
			'last_login_time' => time(),
		];
		// dump($data);exit;
		$re = db('member')->where(['id'=>$uid])->update($data);
		$user_info = $this->get_user($uid);
		if($re){
			return $this->return_success('保存成功!',$user_info,1);
		}else{
			return $this->return_error('保存失败!',[],0);
		}
	}
}
